﻿namespace GuiaTest
{
    using System;
    using System.Globalization;
    using System.IO;
    using System.Threading;
    using Guia.Core;
    using Guia.Elements;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using WpfTestApplication;

    [TestClass]
    public class SaveFileDialogTest : AbstractWindowTest
    {
        private const string TestFile = "test.txt";

        [TestMethod]
        public void TestSaveFile()
        {
            Assert.IsNotNull(window);
            window.Get<TextBox>("txtText").Value = "This is a text";
            window.Get<Button>("btnSaveFile").Invoke();
            SaveFileDialog saveDialog = window.GetSaveFileDialog();
            saveDialog.Save(TestFile);
            Delay.ForMilliseconds(100);
            Assert.AreEqual("This is a text", File.ReadAllText(TestFile));
        }

        [TestMethod]
        public void TestSaveFileNullValue()
        {
            Assert.IsNotNull(window);
            window.Get<TextBox>("txtText").Value = "This is a text";
            window.Get<Button>("btnSaveFile").Invoke();
            SaveFileDialog saveDialog = window.GetSaveFileDialog();
            try
            {
                saveDialog.Save(null);
                Assert.Fail();
            }
            catch (ArgumentNullException)
            {
                Assert.IsTrue(true);
            }

            saveDialog.Cancel();
        }

        [TestMethod]
        public void TestSaveFileTitle()
        {
            Assert.IsNotNull(window);
            window.Get<TextBox>("txtText").Value = "This is a text";
            window.Get<Button>("btnSaveFileTitle").Invoke();
            SaveFileDialog saveDialog = window.GetSaveFileDialog("Save my file");
            saveDialog.Save(TestFile);
            Delay.ForMilliseconds(100);
            Assert.AreEqual("This is a text", File.ReadAllText(TestFile));
        }

        [TestMethod]
        public void TestSaveFileWrongTitle()
        {
            Assert.IsNotNull(window);
            window.Get<Button>("btnSaveFileTitle").Invoke();
            SaveFileDialog saveDialog;
            try
            {
                window.GetSaveFileDialog();
                Assert.Fail();
            }
            catch (ElementNotFoundException)
            {
                Assert.IsTrue(true);
            }
            finally
            {
                saveDialog = window.GetSaveFileDialog("Save my file");
                saveDialog.Cancel();
            }
        }

        [TestMethod]
        public void TestSaveFileCancel()
        {
            Assert.IsNotNull(window);
            window.Get<TextBox>("txtText").Value = "This is a text";
            window.Get<Button>("btnSaveFile").Invoke();
            SaveFileDialog saveDialog = window.GetSaveFileDialog();
            saveDialog.Cancel();
            Delay.ForMilliseconds(100);
            Assert.IsFalse(File.Exists(TestFile));
        }

        [TestCleanup]
        public void CleanUpFile()
        {
            if (File.Exists(TestFile))
            {
                File.Delete(TestFile);
            }
        }

        protected override Type TestWindowType()
        {
            return typeof(SaveFileDialogTestWindow);
        }

        protected override string TestWindowName()
        {
            return "SaveFileDialogTestWindow";
        }
    }
}
